package cn.seqdata.cxf.provider;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.Period;

import cn.seqdata.cxf.converter.DateTimeConverter;
import cn.seqdata.cxf.converter.LocalDateConverter;
import cn.seqdata.cxf.converter.LocalDateTimeConverter;
import cn.seqdata.cxf.converter.LocalTimeConverter;
import cn.seqdata.cxf.converter.PeriodConverter;

@Provider
public class JodaConverterProvider implements ParamConverterProvider {

	@SuppressWarnings("unchecked")
	@Override
	public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
		if (DateTime.class.isAssignableFrom(rawType)) {
			return (ParamConverter<T>) new DateTimeConverter();
		} else if (LocalDate.class.isAssignableFrom(rawType)) {
			return (ParamConverter<T>) new LocalDateConverter();
		} else if (LocalTime.class.isAssignableFrom(rawType)) {
			return (ParamConverter<T>) new LocalTimeConverter();
		} else if (LocalDateTime.class.isAssignableFrom(rawType)) {
			return (ParamConverter<T>) new LocalDateTimeConverter();
		} else if (Period.class.isAssignableFrom(rawType)) {
			return (ParamConverter<T>) new PeriodConverter();
		} else {
			return null;
		}
	}
}
